package com.flyu.service.provider;

import com.flyu.dao.BaseDao;
import com.flyu.dao.Provider.ProviderDao;
import com.flyu.dao.Provider.ProviderDaoImpl;
import com.flyu.pojo.Provider;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ProviderServiceImpl implements ProviderService{
    // 调用Dao层
    private ProviderDao providerDao;
    public ProviderServiceImpl() {
        providerDao = new ProviderDaoImpl();
    }

    public List<Provider> getProviderList() {
        Connection connection = null;
        List<Provider> roleList = null;
        try {
            connection = BaseDao.getConnection();
            roleList = providerDao.getProviderList(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return roleList;
    }

    public List<Provider> getProviderListByRule(String queryProCode, String queryProName, int currentPageNo, int pageSize){
        Connection connection = null;
        List<Provider> providerList = null;
        try {
            connection = BaseDao.getConnection();
            providerList = providerDao.getProviderListByRule(connection, queryProCode, queryProName, currentPageNo, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return providerList;
    }
    // 统计符合条件的供应商数量
    public int getProviderCount(String providerCode, String providerName) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = providerDao.getProviderCount(connection, providerCode, providerName);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return count;
    }

    // 添加供货商
    public boolean addProvider(Provider provider) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            int i = providerDao.addProvider(connection, provider);
            if (i > 0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    // 删除供应商
    public boolean delProvider(int proid) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            int i = providerDao.delProvider(connection, proid);
            if (i > 0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    // 根据id返回对应provider
    public Provider getProviderById(int proid) {
        Provider provider = null;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            provider = providerDao.getProviderById(connection, proid);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return provider;
    }
    // 保存修改
    public boolean modifySave(Provider provider) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            int i = providerDao.modifySave(connection, provider);
            System.out.println(i);
            if (i > 0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Test
    public void test(){
        ProviderServiceImpl providerService = new ProviderServiceImpl();
        Provider provider = new Provider();
        provider.setProCode("test_code");
        provider.setProName("test_name111");
        provider.setProContact("test_concat");
        boolean b = providerService.modifySave(provider);
        System.out.println(b);
    }
}
